package com.nightpiter.view.drawer.advanced;

import java.util.ArrayList;
import java.util.Collection;

import com.nightpiter.model.objects.Station;
import com.nightpiter.model.objects.Tunnel;



/**
 * 
 * Some methods to prepare tunnels for drawing
 * 
 * @author Alexandr Melamud
 * 
 */

public class TunnelResolver {	
	
	public static Collection<Tunnel> formParallelTunnels (Station station) {
	
		Collection<Tunnel> parallelTunnels = new ArrayList<Tunnel>();
		
		for (Tunnel tunnel: station.getTunnels()){
			for (Tunnel tunnelPar: station.getTunnels()){
				if (!(parallelTunnels.contains(tunnelPar))){
					if ((!tunnelPar.equals(tunnel))) {
						if ((!tunnel.equals(tunnelPar))){
							if ((tunnel.getToStation().equals(tunnelPar.getToStation()))
							& (tunnel.getFromStation().equals(tunnelPar.getFromStation()))) {
								parallelTunnels.add(tunnelPar);
							}
						}
					}
					
				}
			}
		}
	
		return parallelTunnels;	
	}
	
	public static Tunnel backTunnel (Tunnel tunnel){		
		
		for (Tunnel backTunnel: tunnel.getToStation().getTunnels()){
			if ( (backTunnel.getToStation().equals(tunnel.getFromStation()))
				& (backTunnel.getLine().equals(tunnel.getLine()))){
				return backTunnel;
			}
		}		
		return null;		
	}
	
	public Collection<DrawingTunnel> formParallelTunnels 
	(DrawingTunnel tunnel,Collection<DrawingTunnel> drawingTunnels ) {
		Collection<DrawingTunnel> parallelTunnels = new ArrayList<DrawingTunnel> ();		
		parallelTunnels.add(tunnel);
		
		for (DrawingTunnel drawingTunnel: drawingTunnels) {
			if ((drawingTunnel.getTunnel().getToStation()==tunnel.getTunnel().getToStation())&
					(drawingTunnel.getTunnel().getFromStation()==tunnel.getTunnel().getFromStation())){
				parallelTunnels.add(drawingTunnel);
			}
		}
		
		return parallelTunnels;		
	}
	
	
	public static int getNumberOfDirectionsOfParallelTunnels (Collection<DrawingTunnel> parallelTunnels){		
		
		int i=1;
		Collection<Station> toStations = new ArrayList<Station>();
		
		for (DrawingTunnel tunnel: parallelTunnels){
			for (DrawingTunnel tunnelPar: parallelTunnels) {
				if (!tunnel.getTunnel().getToStation().equals(tunnelPar.getTunnel().getToStation())) {
					if (!toStations.contains(tunnel.getTunnel().getToStation())){
						toStations.add(tunnel.getTunnel().getToStation());						
					}
				}
			}
		}	
		
		if (toStations.size()>=1){
			i=toStations.size();
		}
		
		return i;		
	}
	
}
